package com.stx.openeyes.view.fragment;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TabHost;

import com.ogaclejapan.smarttablayout.SmartTabLayout;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItem;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItemAdapter;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItems;
import com.stx.openeyes.R;
import com.stx.openeyes.SealConst;
import com.stx.openeyes.utils.ContextUtil;
import com.stx.openeyes.utils.NToast;
import com.stx.openeyes.view.activity.NavigationActivity;
import com.stx.openeyes.view.activity.SearchActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 热门排行
 */
public class HotFragment extends Fragment {
    //标题
    public static final String[] TITLE = new String[]{"关注", "推荐", "功课", "音乐", "舞蹈", "运动", "摄影", "健身", "游戏", "付费"};

    @BindView(R.id.hot_viewpager)
    ViewPager hotViewpager;
    @BindView(R.id.viewpagertab)
    SmartTabLayout viewpagertab;
    @BindView(R.id.im_show_more)
    ImageView showMore;
    private List<Fragment> fragments = new ArrayList<>();
    protected Context mContext;
    FragmentPagerItemAdapter adapter = null;

    public HotFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_hot, container, false);
        ButterKnife.bind(this, view);
        mContext = getActivity();
        initView();
        initData();
        setAdapter();
        return view;
    }

    //初始化控件
    private void initView() {

    }

    //初始化数据
    private void initData() {
        //循环创建布局
        for (int i = 0; i < TITLE.length; i++) {
            CommonHotFragment commonHotFragment = new CommonHotFragment();
            fragments.add(commonHotFragment);
        }
        hotViewpager.setOffscreenPageLimit(TITLE.length);
        hotViewpager.addOnAdapterChangeListener(new ViewPager.OnAdapterChangeListener() {
            @Override
            public void onAdapterChanged(@NonNull ViewPager viewPager, @Nullable PagerAdapter oldAdapter, @Nullable PagerAdapter newAdapter) {
                Log.i("onAdapterChanged", "---" + newAdapter.getCount());
            }
        });
    }

    //设置适配器
    private void setAdapter() {
        //实例化适配器
        adapter = new FragmentPagerItemAdapter(getFragmentManager(), FragmentPagerItems.with(getContext())
                .add(TITLE[0], fragments.get(0).getClass())
                .add(TITLE[1], fragments.get(1).getClass())
                .add(TITLE[2], fragments.get(2).getClass())
                .add(TITLE[3], fragments.get(3).getClass())
                .add(TITLE[4], fragments.get(4).getClass())
                .add(TITLE[5], fragments.get(5).getClass())
                .add(TITLE[6], fragments.get(6).getClass())
                .add(TITLE[7], fragments.get(7).getClass())
                .add(TITLE[8], fragments.get(8).getClass())
                .add(TITLE[9], fragments.get(9).getClass())
                .create());
        //设置适配器
        hotViewpager.setAdapter(adapter);
        // 默认展示推荐
        hotViewpager.setCurrentItem(1);

        hotViewpager.addOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position < 2) {
                    notifyResume(position);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        viewpagertab.setViewPager(hotViewpager);

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    private boolean first = true;
    @Override
    public void onResume() {
        if (ContextUtil.tabIndex != null) {
            hotViewpager.setCurrentItem(ContextUtil.tabIndex);
            ContextUtil.tabIndex = null;
        }

        super.onResume();
        if (!first && hotViewpager.getCurrentItem() < 2) {
            notifyResume(hotViewpager.getCurrentItem());
        }
        first = false;
    }

    private void notifyResume(int position) {
        CommonHotFragment item = (CommonHotFragment) adapter.getPage(position);
        item.callResume(position);
    }

    @OnClick({R.id.im_show_more})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.im_show_more:
                startActivity(new Intent(getContext(), NavigationActivity.class));
                break;
        }
    }
}
